//
//  ShareItemPopViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/8/22.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "ShareItemCell.h"
#import "ShareItemModel.h"
#import "ShareItemPopViewController.h"

@interface ShareItemPopViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray <ShareItemModel *> *shareItemListArray;

@property (nonatomic ,strong) UIViewController *vc;

@property (nonatomic, assign) NSInteger defaultIndex;
@property (nonatomic, assign) CGFloat   defaultHeight;

@property (nonatomic, strong) UIView *headerView;

@end

@implementation ShareItemPopViewController

- (instancetype)initDefaultIndex:(NSInteger)index defaultHeight:(CGFloat)height vc:(UIViewController *)vc dataSource:(NSMutableArray <ShareItemModel *>*)dataSource {
    
    self.shareItemListArray = @[].mutableCopy;
    if (self = [super init]) {
        self.vc = vc;
        self.shareItemListArray = dataSource.mutableCopy;
        self.defaultIndex = index;
        self.defaultHeight = height;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initPop];
    [self setUpUI];
    
    [self.tableView reloadData];
}

- (void)initPop {
    self.view.backgroundColor = [UIColor whiteColor];
    //    CGFloat height = 140;
    //    height += self.shareItemListArray.count * 60;
    
//    self.defaultHeight = self.shareItemListArray.count * 60 + 50  + 60;
    
    if (self.defaultHeight > TTScreenHeight / 2) {
        self.defaultHeight = TTScreenHeight / 2;
    }
    
    self.contentSizeInPopup = CGSizeMake(TTScreenWidth, self.defaultHeight);
    self.popupController.navigationBarHidden = YES;
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)]];
}

- (void)setUpUI {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headerView];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(50);
    }];
    
}

-(void)closeBlockView {
    [self backgroundTap];
}

- (void)backgroundTap  {
    [self.popupController dismiss];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor blackColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        //银行卡cell
        [_tableView registerNib:[UINib nibWithNibName:@"ShareItemCell" bundle:nil] forCellReuseIdentifier:@"ShareItemCellID"];
        
    }
    return _tableView;
}

- (UIView *)headerView {
    if (!_headerView) {
        
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TTScreenWidth, 50)];
        _headerView.backgroundColor = [UIColor whiteColor];;
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = PP_Default_Color_Gray_F1F1F1;
        [_headerView addSubview:lineView];
        
        UILabel *title = [[UILabel alloc] init];
        title.text = @"分享";
        title.textAlignment = NSTextAlignmentCenter;
        [_headerView addSubview:title];
        
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(0);
        }];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_equalTo(1);
        }];
    }
    return _headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShareItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShareItemCellID" forIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    
    //触发隐藏及传出下标
    /*
     [self closeBlockView];
     
     if (self.selectIndex) {
     self.selectIndex(indexPath.row);
     }
     */
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

}










@end
